<?php

class phpinchains_util_db_JoinTable {

    protected $class;
    protected $connectedClass;
    protected $thisfield;
    protected $otherfield;
    /**
     *
     * @var phpinchains_util_db_DbEntityBase Entity
     */
    protected $entity;
    /**
     *
     * @var phpinchains_util_db_DbEntityBase Entity
     */
    protected $connectedEntity;

    public function  __construct($class, $connectedClass, $thisfield, $otherfield) {
        $this->class      = $class;
        $this->connectedClass = $connectedClass;
        $this->thisfield  = $thisfield;
        $this->otherfield = $otherfield;
    }

    public function init() {
        if (!class_exists($this->class)) throw new phpinchains_util_db_EntityException ('Class does not exist: '.$this->class);
        $class = $this->class;
        $this->entity          = new $class(new phpinchains_ActionForm());
        if (!class_exists($this->connectedClass)) throw new phpinchains_util_db_EntityException ('Class does not exist: '.$this->connectedClass);
        $connectedClass = $this->connectedClass;
        $this->connectedEntity = new $connectedClass(new phpinchains_ActionForm());
    }

    public function getThisField() {
        return $this->thisfield;
    }

    public function getOtherField() {
        return $this->otherfield;
    }

    /**
     *
     * @param string $field Field to check existence
     */
    public function hasField ($field) {
        $this->connectedEntity->hasField($field);
    }

    /**
     *
     * @param int $id         Id of the main table
     * @param array $values   Array of ids of the foreign table, conneted via connection table
     * @param bool $showquery Show generated query?
     */
    public function create($id, $values, $showquery = false) {
        if (is_array($values))
        foreach ($values as $value) {
            $this->entity->create(array(
                $this->thisfield => $id,
                $this->otherfield => $value
            ), $showquery);
        }
    }

    public function delete($id, $showquery = false) {
        $this->entity->delete($id, $showquery);
    }

    /**
     *
     * @param int $id         Id of the main table
     * @param array $fields   Array of ids of the foreign table, conneted via connection table
     * @param bool $showquery Show generated query?
     */
    public function update($id, $values, $showquery = false) {
        if (is_array($values)) {
            // connection table has no key, but first column configured in DbEntityBase is used by delete
            $this->entity->deleteByField($this->thisfield, $id, $showquery);
            foreach ($values as $value)
                $this->entity->create(array(
                    $this->thisfield => $id,
                    $this->otherfield => $value
                ), $showquery);
        }
    }

    /**
     *
     * @param int    $id        Id of the main table
     * @param string $showquery Show generated query?
     */
    public function findAll ($id, $showquery = false) {
        $result = $this->entity->findBy($this->thisfield, $id, array('showquery' => $showquery, 'join' => $this->connectedEntity->getTableName()));
        $res = array();
        for ($i = 0; $i < count($result); $i++) {
            $res[] = $result[$i][$this->otherfield];
        }
        return $res;
    }

}